import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;

@WebServlet("/hello")
public class login_system extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession(false);
        if (session==null){
            resp.setStatus(404);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("请登录后再试");
        }
        resp.setStatus(200);
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("欢迎来到主页"+session.getAttribute("userid"));
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String userid=req.getParameter("userid");
        String password=req.getParameter("password");
        try {
            if (method(userid, password)) {
                HttpSession session= req.getSession(true);
                session.setAttribute("userid",userid);
                resp.sendRedirect("hello");
            }else {
                resp.setStatus(404);
                resp.setContentType("text/html; charset=utf8");
                resp.getWriter().write("登陆失败,请重试");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean method(String userid,String password) throws SQLException {
        if(userid==null||password==null){
            return false;
        }
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/login?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("666666");

        Connection connection=dataSource.getConnection();

        String sql="select * from user";
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet resultSet=statement.executeQuery();

        while (resultSet.next()){
            if (userid.equals(resultSet.getString("userid"))){
                if (password.equals(resultSet.getString("password"))){
                    resultSet.close();
                    statement.close();
                    connection.close();
                    return true;
                }
            }
        }
        resultSet.close();
        statement.close();
        connection.close();
        return false;
    }
}
